/*
 * Copyright (C) 2013 Michael
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.jm.jmsql.database;

import com.jm.commons.ui.UIBounds;
import com.jm.jmsql.models.JDBCTypeComboBoxModel;
import com.jm.jmsql.models.JDBCTypeComboBoxModel.JDBCType;
import com.jm.jmsql.objects.Column;
import java.awt.Frame;

/**
 *
 * @created Mar 15, 2013
 * @author Michael
 */
public class CreateColumn extends javax.swing.JDialog {
    
    /**
     * 
     */
    private boolean done = false;
    
    /**
     * 
     */
    private int index;
    
    /**
     * Creates new form CreateColumn
     * @param parent
     * @param index
     */
    public CreateColumn(Frame parent, int index) {
        super(parent);
        initComponents();
        UIBounds.setBounds(parent, this);
        this.mainPanel.setImage(new java.io.File("./images/optionslogo.png"));
        /* Set the icon image
        try {
            setIconImage(ImageIO.read(getClass().getClassLoader().getResource("com/jm/jmsql/icons/create_column.png")));
        } catch (IOException ioe) {
            LogSystem.log(getClass(), getClass(), "Create Icon", ioe);
        }
        *///
        this.index = index;
        this.cbxTypes.setModel(new JDBCTypeComboBoxModel());
        this.cbxTypes.setSelectedIndex(1);
    }
    
    /**
     * 
     * @return 
     */
    public boolean isDone() {
        return this.done;
    }

    /**
     *
     * @return
     */
    public Column getColumn() {
        return new Column(null,
                this.tfName.getText(),
                Column.JdbcJavaType.get(((JDBCTypeComboBoxModel.JDBCType) this.cbxTypes.getSelectedItem()).getType()).getType(),
                ((JDBCTypeComboBoxModel.JDBCType) this.cbxTypes.getSelectedItem()).getTypeName(),
                (int) ((Number) this.spinLength.getValue()),
                (int) ((Number) this.spinDecimals.getValue()),
                this.index,
                this.chbPrimaryKey.isSelected(),
                this.chbNullable.isSelected(),
                this.chbIncremented.isSelected(),
                this.chbGenerated.isSelected());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new com.jm.commons.components.panel.ImagePanel();
        tfName = new com.jm.commons.components.textfield.HintJTextField();
        cbxTypes = new javax.swing.JComboBox();
        spinLength = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        spinDecimals = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        chbPrimaryKey = new javax.swing.JCheckBox();
        chbNullable = new javax.swing.JCheckBox();
        chbIncremented = new javax.swing.JCheckBox();
        chbGenerated = new javax.swing.JCheckBox();
        btnDone = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Create Column");
        setModal(true);
        setResizable(false);

        mainPanel.setImage(new java.io.File("C:\\Users\\Michael\\Documents\\NetBeansProjects\\JMSql\\images\\optionslogo.png"));

        tfName.setHintText("Name...");
        tfName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                setName(evt);
            }
        });

        cbxTypes.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        cbxTypes.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                selectType(evt);
            }
        });

        spinLength.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        jLabel1.setText("Length");

        spinDecimals.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        jLabel2.setText("Decimals");

        chbPrimaryKey.setText("Primary Key");
        chbPrimaryKey.setOpaque(false);
        chbPrimaryKey.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                setPrimaryKey(evt);
            }
        });

        chbNullable.setText("Nullable");
        chbNullable.setOpaque(false);

        chbIncremented.setText("Incremented");
        chbIncremented.setOpaque(false);

        chbGenerated.setText("Generated");
        chbGenerated.setOpaque(false);

        btnDone.setText("Done");
        btnDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDoneActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbxTypes, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(spinLength, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                            .addComponent(spinDecimals)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnDone))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chbGenerated)
                            .addComponent(chbIncremented)
                            .addComponent(chbNullable)
                            .addComponent(chbPrimaryKey))
                        .addGap(0, 197, Short.MAX_VALUE)))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addGap(89, 89, 89)
                .addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbxTypes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinLength, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinDecimals, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chbPrimaryKey)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chbNullable)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chbIncremented)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chbGenerated)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnDone)
                .addGap(14, 14, 14))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * 
     * @param evt 
     */
    private void selectType(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_selectType
        if (this.cbxTypes.getSelectedItem() != null &&
                this.cbxTypes.getSelectedItem() instanceof JDBCType) {
            //
            if (((JDBCType) this.cbxTypes.getSelectedItem()).hasNoLength()) {
                this.spinLength.setValue(0);
                this.spinLength.setEnabled(false);
            } else {
                this.spinLength.setEnabled(true);
            }
            //
            if (((JDBCType) this.cbxTypes.getSelectedItem()).hasDecimals()) {
                this.spinDecimals.setEnabled(true);
            } else {
                this.spinDecimals.setValue(0);
                this.spinDecimals.setEnabled(false);
            }
        }
    }//GEN-LAST:event_selectType
    
    /**
     * 
     * @param evt 
     */
    private void btnDoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDoneActionPerformed
        this.done = true;
        setVisible(false);
        dispose();
    }//GEN-LAST:event_btnDoneActionPerformed
    
    /**
     * 
     * @param evt 
     */
    private void setName(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_setName
        if (!Character.isLetterOrDigit(evt.getKeyChar()) &&
                evt.getKeyChar()  != '_' &&
                    evt.getKeyChar() != '@' &&
                        this.tfName.getText().length() >= 25) {
            evt.consume();
        }
    }//GEN-LAST:event_setName

    private void setPrimaryKey(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_setPrimaryKey
        this.chbNullable.setEnabled(!this.chbPrimaryKey.isSelected());
        if (this.chbPrimaryKey.isSelected()) {
            this.chbNullable.setSelected(false);
        }
    }//GEN-LAST:event_setPrimaryKey
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDone;
    private javax.swing.JComboBox cbxTypes;
    private javax.swing.JCheckBox chbGenerated;
    private javax.swing.JCheckBox chbIncremented;
    private javax.swing.JCheckBox chbNullable;
    private javax.swing.JCheckBox chbPrimaryKey;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private com.jm.commons.components.panel.ImagePanel mainPanel;
    private javax.swing.JSpinner spinDecimals;
    private javax.swing.JSpinner spinLength;
    private com.jm.commons.components.textfield.HintJTextField tfName;
    // End of variables declaration//GEN-END:variables
}
